package v1

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"teacher-helper/global"
	"teacher-helper/models"
	"teacher-helper/models/request"
	"teacher-helper/service"
)

func GetStuRewardList(c *gin.Context) {
	var queryInfo request.QueryInfo
	if err := c.ShouldBind(&queryInfo); err != nil {
		c.JSON(201, gin.H{"msg": err, "data": nil})
		return
	}
	StuRewards, total, err := service.GetStuRewardList(queryInfo)
	if err != nil {
		c.JSON(202, gin.H{"msg": err, "data": nil})
	} else {
		c.JSON(http.StatusOK, gin.H{"msg": "获取学生获奖列表成功", "data": map[string]interface{}{
			"stu_rewards": StuRewards,
			"total": total,
		}})
	}
	return
}


func AddOneStuReward(c *gin.Context)  {
	var StuReward models.SysStuReward
	if err := c.ShouldBindJSON(&StuReward);err != nil{
		global.Logger.Error(err.Error())
		c.JSON(201, gin.H{"msg": err})
		return
	}
	err := service.AddOneStuReward(StuReward)
	if err != nil {
		c.JSON(202, gin.H{"msg": err})
	}else {
		c.JSON(http.StatusOK, gin.H{"msg": "添加学生获奖成功"})
	}
	return
}

func UpdateStuReward(c *gin.Context) {
	var StuReward models.SysStuReward
	if err := c.BindJSON(&StuReward); err != nil {
		c.JSON(201, gin.H{"msg": "参数获取失败"})
		return
	}
	err := service.UpdateStuReward(StuReward)
	if err != nil {
		c.JSON(201, gin.H{"msg": err, "data": nil})
	} else {
		c.JSON(http.StatusOK, gin.H{"msg": "修改学生获奖成功", "data": nil})
	}
	return
}

func DeleteStuReward(c *gin.Context) {
	id, ok := c.GetQuery("id")
	Id, err := strconv.Atoi(id)
	if !ok || err != nil {
		c.JSON(201, gin.H{"msg": "参数获取失败"})
		return
	}
	err = service.DeleteStuReward(Id)
	if err != nil {
		c.JSON(201, gin.H{"msg": err, "data": nil})
	} else {
		c.JSON(http.StatusOK, gin.H{"msg": "删除学生获奖成功", "data": nil})
	}
	return
}



